import Layout from './layout'
import { isJSON } from '@/utils/isJson'
import { returnData } from '@/utils/dataType'
export { Layout }

// 判断日期
export function dateFunc(msg) {
	if (msg && new Date(msg).toDateString() !== 'Invalid Date') {
		const moon = new Date(msg).getMonth() // 判断是否当月
		const year = new Date().getFullYear() - new Date(msg).getFullYear()

		const current = new Date().getTime() - new Date(msg).getTime()
		const Minutes = 60 * 1000 // 1分时间戳
		const Hours = 60 * 60 * 1000 // 1小时时间戳
		const sky = 24 * 60 * 60 * 1000 // 1天时间戳
		const Month = 30 * 24 * 60 * 60 * 1000 // 1月时间戳
		const therMonth = 3 * 30 * 24 * 60 * 60 * 1000 // 3月时间戳
		const SixMonths = 6 * 30 * 24 * 60 * 60 * 1000 // 半年时间戳
		const twelveMonths = 12 * 30 * 24 * 60 * 60 * 1000 // 一年时间戳
		if (year === 0) {
			if (current < twelveMonths && current >= SixMonths) {
				return `半年前`
			}
			if (current < SixMonths && current >= therMonth) {
				return `三个月前`
			}
			if (current < therMonth && current >= Month) {
				return `一个月前`
			}
			if (current < Month) {
				// 当月
				if (current <= sky) {
					// 当天
					if (current <= Minutes) {
						const s = new Date().getTime() - new Date(msg).getTime()
						return `${Math.round(s / 1000)}秒前`
					} else if (current <= Hours) {
						const m = new Date().getTime() - new Date(msg).getTime()
						return `${Math.round(m / Minutes)}分前`
					} else {
						const h = new Date().getTime() - new Date(msg).getTime()
						return `${Math.round(h / Hours)}小时前`
					}
				} else {
					return `${Math.round((new Date().getTime() - new Date(msg).getTime()) / sky)}天前`
				}
			}
		} else if (year === 1) {
			if (moon > new Date().getMonth()) {
				if ((new Date().getMonth() === 0 && [10, 11].includes(moon)) || (new Date().getMonth() === 1 && [11].includes(moon))) {
					return `一个月前`
				} else if (
					(new Date().getMonth() === 2 && [11].includes(moon)) ||
					(new Date().getMonth() === 1 && [10].includes(moon)) ||
					(new Date().getMonth() === 0 && [9].includes(moon))
				) {
					return `三个月前`
				} else {
					return `半年前`
				}
			} else {
				return `${year}年前`
			}
		} else {
			if (year) {
				return `${year}年前`
			} else {
				return `${Math.abs(year)}年后`
			}
		}
	}
}

// 判断是否需要提示框
export function isCharCodeAt(msg) {
	if (!msg) {
		return {
			isOverflow: false
		}
	}
	const list = [...msg]
	let lengthCodeAt = 0
	let isOverflow = false
	list.forEach((item, key) => {
		if (lengthCodeAt < 156) {
			if (item.toString().charCodeAt(0) > 255) { // 中文字符
				lengthCodeAt += 2
			} else if (item.toString().charCodeAt(0) < 48 || item.toString().charCodeAt(0) > 57) { // 数字
				lengthCodeAt += 0.8
			} else {
				lengthCodeAt += 1
			}
		} else {
			isOverflow = true
		}
	})

	return {
		isOverflow
	}
}

export function otherMapType(item) {
	if (isJSON(item.msgBody.otherMap)) {
		const obj = JSON.parse(item.msgBody.otherMap)
		if (returnData(obj) === 'Object') {
			if (obj.windowType === '2') { // 原窗口打开
				location.href = obj.linkUrl
			} else if (obj.windowType === '1') { // 新窗口打开
				window.open(obj.linkUrl, '_seft')
			}
		}
	}
}

export const footer = {
	links: [
		{
			title: 'IGW'
		},
		{
			title: 'Github'
		}
	],
	copyright: {
		title: '2020 igwfmc copyright'
	}
}

